﻿
// EmployeeManagementDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "EmployeeManagement.h"
#include "EmployeeManagementDlg.h"
#include "afxdialogex.h"
#include "CAddDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框



// CEmployeeManagementDlg 对话框



CEmployeeManagementDlg::CEmployeeManagementDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_EMPLOYEEMANAGEMENT_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEmployeeManagementDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, m_showList);
}

BEGIN_MESSAGE_MAP(CEmployeeManagementDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(ID_add, &CEmployeeManagementDlg::Onadd)
	ON_COMMAND(ID_modify, &CEmployeeManagementDlg::Onmodify)
	ON_COMMAND(ID_remove, &CEmployeeManagementDlg::Onremove)
	ON_WM_CLOSE()
END_MESSAGE_MAP()


// CEmployeeManagementDlg 消息处理程序

BOOL CEmployeeManagementDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	m_showList.InsertColumn(0, _T("工号"), LVCFMT_LEFT, 100);//插入列，设置列宽，居左对齐
	m_showList.InsertColumn(1, _T("姓名"), LVCFMT_LEFT, 100);
	m_showList.InsertColumn(2, _T("性别"), LVCFMT_LEFT, 100);
	m_showList.InsertColumn(3, _T("年龄"), LVCFMT_LEFT, 100);
	m_showList.InsertColumn(4, _T("部门"), LVCFMT_LEFT, 100);
	m_showList.InsertColumn(5, _T("职位"), LVCFMT_LEFT, 100);
	m_showList.InsertColumn(6, _T("入职时间"), LVCFMT_LEFT, 100);
	m_showList.InsertColumn(7, _T("工资"), LVCFMT_LEFT, 100);
	m_showList.InsertColumn(8, _T("联系方式"), LVCFMT_LEFT, 100);
	m_showList.InsertColumn(9, _T("住址"), LVCFMT_LEFT, 100);
	m_showList.InsertColumn(10, _T("兴趣爱好"), LVCFMT_LEFT, 100);

	m_showList.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);//设置列表风格,整行选和网格线

	m_document.OnOpenDocument(_T("employee.txt"));//打开文件
	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}


void CEmployeeManagementDlg::OnSysCommand(UINT nID, LPARAM lParam)
{

	CDialogEx::OnSysCommand(nID, lParam);
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CEmployeeManagementDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CEmployeeManagementDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CEmployeeManagementDlg::addMyList(CAddDlg& dlg, int row)
{
	CString str;
	str.Format(_T("%d"), dlg.m_jobid);
	m_showList.SetItemText(row, 0, str);
	m_showList.SetItemText(row, 1, dlg.m_name);
	str = dlg.m_sex == 0 ? _T("男") : _T("女");
	m_showList.SetItemText(row, 2, str);
	str.Format(_T("%d"), dlg.m_age);//年龄
	m_showList.SetItemText(row, 3, str);
	m_showList.SetItemText(row, 4, dlg.m_department);//部门
	m_showList.SetItemText(row, 5, dlg.m_job);//职位
	str = dlg.m_entyTime.Format(_T("%Y-%m-%d"));//入职时间
	m_showList.SetItemText(row, 6, str);
	str.Format(_T("%d"), dlg.m_salary);//工资
	m_showList.SetItemText(row, 7, str);
	m_showList.SetItemText(row, 8, dlg.m_tel);//联系方式
	m_showList.SetItemText(row, 9, dlg.m_adress);//住址
	m_showList.SetItemText(row, 10, dlg.m_interest);//兴趣爱好
}

CAddDlg CEmployeeManagementDlg::readMyList(int row)
{
	CAddDlg dlg;
	CString str;
	dlg.m_jobid = _ttoi(m_showList.GetItemText(row, 0));//工号
	dlg.m_name = m_showList.GetItemText(row, 1);//姓名
	dlg.m_sex = m_showList.GetItemText(row, 2) == _T("男") ? 0 : 1;//性别
	dlg.m_age = _ttoi(m_showList.GetItemText(row, 3));//年龄
	dlg.m_department = m_showList.GetItemText(row, 4);//部门
	dlg.m_job = m_showList.GetItemText(row, 5);//职位
	str = m_showList.GetItemText(row, 6);//入职时间
	dlg.m_entyTime.ParseDateTime(str);
	dlg.m_salary = _ttoi(m_showList.GetItemText(row, 7));//工资
	dlg.m_tel = m_showList.GetItemText(row, 8);//联系方式
	dlg.m_adress = m_showList.GetItemText(row, 9);//住址
	dlg.m_interest = m_showList.GetItemText(row, 10);//兴趣爱好
	if (dlg.m_interest.Find(_T("唱歌")) != -1)
		dlg.m_sing = TRUE;
	if (dlg.m_interest.Find(_T("跳舞")) != -1)
		dlg.m_dance = TRUE;
	if (dlg.m_interest.Find(_T("说唱")) != -1)
		dlg.m_rap = TRUE;
	return dlg;
}




void CEmployeeManagementDlg::Onadd()
{
	CAddDlg dlg;
	if (dlg.DoModal() == IDCANCEL)
		return;
	int n = m_showList.GetItemCount();//获取当前行数
	m_showList.InsertItem(n, _T(""));//插入一行
	addMyList(dlg, n);//添加到列表
}


void CEmployeeManagementDlg::Onmodify()
{
	//获取选中的行
	int n = m_showList.GetNextItem(-1, LVNI_SELECTED);
	if (n == -1)
	{
		MessageBox(_T("请选择要修改的员工信息"));
		return;
	}
	CAddDlg dlg;
	dlg = readMyList(n);//读取当前行信息
	if (dlg.DoModal() == IDCANCEL)
		return;
	//修改员工信息
	addMyList(dlg, n);

}


void CEmployeeManagementDlg::Onremove()
{
	//获取选中的行
	int n = m_showList.GetNextItem(-1, LVNI_SELECTED);
	if (n == -1)
	{
		MessageBox(_T("请选择要删除的员工信息"));
		return;
	}
	//再次提示
	if (MessageBox(_T("确定删除吗？"), _T("提示"), MB_OKCANCEL) == IDCANCEL)
		return;
	m_showList.DeleteItem(n);//删除当前行
}




void CEmployeeManagementDlg::OnClose()
{
	m_document.OnSaveDocument(_T("employee.txt"));
	CDialogEx::OnClose();
}

void CEmployeeManagementDlg::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{	//读取总行数
		int n = m_showList.GetItemCount();
		//获取总列数
		int m = m_showList.GetHeaderCtrl()->GetItemCount();
		ar << n;//总行数
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
				ar << m_showList.GetItemText(i, j);//写入每行信息
		}
	}
	else
	{
		//读取文件
		int n;
		ar >> n;//总行数
		//获取总列数
		int m = m_showList.GetHeaderCtrl()->GetItemCount();
		CString str;//临时字符串
		for (int i = 0; i < n; i++)
		{
			m_showList.InsertItem(i, _T(""));//插入一行
			for (int j = 0; j < m; j++)
			{
				ar >> str;
				m_showList.SetItemText(i, j, str);
			}
		}
	}
}
